package com.example.recyclerview01.baseuse;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.recyclerview01.R;
import com.example.recyclerview01.wrapper.WrapRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class BaseUseActivity extends AppCompatActivity {

    private RecyclerAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_use);
        initData();


        WrapRecyclerView mRecyclerView = (WrapRecyclerView) findViewById(R.id.recyclerView);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        GridLayoutManager mLayoutManager = new GridLayoutManager(this, 4);
//        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
//                // 设置header item显示的宽度。
//                if (position == 0) return mLayoutManager.getSpanCount();
//                return 1;
//            }
//        });

        mRecyclerView.setLayoutManager(mLayoutManager);
        mListAdapter = new RecyclerAdapter(this, mData);
        mRecyclerView.setAdapter(mListAdapter);

        View header = LayoutInflater.from(this).inflate(R.layout.layout_header_footer, mRecyclerView, false);
        mRecyclerView.addHeaderView(header);
        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.removeHeaderView(header);
            }
        });
        mRecyclerView.addItemDecoration(new GridLayoutItemDecoration(this, R.drawable.item_divider_01));
        // 添加点击事件。
        mListAdapter.setOnItemClickListener(new RecyclerAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(BaseUseActivity.this, "item:" + mData.get(position), Toast.LENGTH_SHORT).show();
                mData.remove(position);
                mListAdapter.notifyDataSetChanged(); // 回调观察者的onChanged方法
//                mListAdapter.notifyItemRemoved(position); // 回调观察者的onItemRangeRemoved方法
            }
        });
    }

    private List<String> mData; // 数据源
    protected void initData() {
        mData = new ArrayList<String>();
        for (int i = 'A'; i <= 'z'; i++) {
            mData.add("" + (char) i);
        }
        Log.d("TAG", "length:" + mData.size());
    }

}